package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.dto.OrderDto;
import cn.itsource.ymcc.jwt.LoginContextUtils;
import cn.itsource.ymcc.query.CourseOrderQuery;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.result.PageList;
import cn.itsource.ymcc.service.ICourseOrderService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    public ICourseOrderService courseOrderService;
    @Autowired
    private LoginContextUtils loginContextUtils;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 幂等性校验：生成token 存储到redis，每次业务执行前先判断Redis中是否存在这个key
     *  存在：则执行业务，后删除这个key
     *  不存在：则不执行业务
     * @return
     */
    @GetMapping("/createToken")
    public JsonResult createToken(){
        String token = UUID.randomUUID().toString();
        //保存到Redis中
        redisTemplate.opsForValue().set(token, 1, 10, TimeUnit.MINUTES);
        return JsonResult.me(token);
    }

    /**
     * 正常下单
     * @param orderDto
     * @return
     */
    @PostMapping("/placeOrder")
    public JsonResult placeOrder(@RequestBody OrderDto orderDto, HttpServletRequest request){
        //获取当前登录用户信息
        Long loginId = loginContextUtils.getLoginUserId(request.getHeader(LoginContextUtils.JWT_TOKEN_NAME));
        orderDto.setLoginId(loginId);
        return JsonResult.me(courseOrderService.placeOrder(orderDto));
    }


    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody CourseOrder courseOrder){
        if(courseOrder.getId()!=null){
            courseOrderService.updateById(courseOrder);
        }else{
            courseOrderService.save(courseOrder);
        }
        return JsonResult.me();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseOrderService.removeById(id);
        return JsonResult.me();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.me().setData(courseOrderService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.me().setData(courseOrderService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseOrderQuery query){
        Page<CourseOrder> page = new Page<CourseOrder>(query.getPageNo(), query.getPageSize());
        page = courseOrderService.page(page);
        return JsonResult.me().setData(new PageList<CourseOrder>(page.getTotal(), page.getRecords()));
    }
}
